<?php

class IndexController extends BackendController
{
    public function actionIndex()
    {
        $this->subnavLinks=$this->createSubMenu($this->hederMainLinks['content']->subMenu,$this->hederMainLinks['content']->url);
        $this->hederMainLinks['content']->id=' id="current"';

        Test::show(Yii::app()->file->image->assetsUrl);

        $criteria=new CDbCriteria();
        $sort=new CSort();
        $sort->sortVar='sort';
        $sort->defaultOrder='id ASC';
        $sort->multiSort=true;
        /*
        $sort->attributes = array(
            'id'=>array(
                'label'=>'id',
                'asc'=>'product_id ASC',
                'desc'=>'product_id DESC',
                'default'=>'desc',
            ),
            'model'=>array(
                'label'=>'цене',
                'asc'=>'model ASC',
                'desc'=>'model DESC',
                'default'=>'desc',
            ),
            'quantity'=>array(
                'label'=>'количеству',
                'asc'=>'quantity ASC',
                'desc'=>'quantity DESC',
                'default'=>'desc',
            ),
            'price'=>array(
                'label'=>'цене',
                'asc'=>'price ASC',
                'desc'=>'price DESC',
                'default'=>'desc',
            ),
        );
        */

        $dataProvider=new CActiveDataProvider($this->model,array(
                'criteria'=>$criteria,
                'sort'=>$sort,
                'pagination'=>array(
                    'pageSize'=>Setting::model()->getConfig('elements_per_page_admin'),
                ),
            )
        );
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
        ));
    }

    public function actionView()
    {
        $model=$this->model->findByPk(Yii::app()->request->getQuery('id'));

        if(Yii::app()->request->isAjaxRequest){
            $this->renderPartial('slider.views.index.__view',array(
                'model'=>$model,
            ));
        }else{
            $this->render('__view',array(
                'model'=>$model,
            ));
        }
    }

    public function actionCreate()
    {
        if(!Yii::app()->request->isAjaxRequest || !Yii::app()->request->getQuery('create')) $this->redirect($this->createUrl('index'));
        $model=$this->model;

        $json=array(
            'htmlContent'=>'',
            'message'=>'',
            'error'=>'',
        );

        $json['htmlContent']=$this->renderPartial('__view',array(
            'model'=>$model,
        ),true,false);

        Yii::app()->end(json_encode($json));
    }

    public function actionDelete($id)
    {
        $model=$this->model->findByPk($id);

        if($model!==null){
            $transaction=Yii::app()->db->beginTransaction();
            $name=$model->slideName;

            try{
                $multiLangModels=$model->getMultiLangModels(true);

                if($multiLangModels){
                    foreach($multiLangModels as $lang){
                        $lang->delete();
                    }
                    unset($multiLangModels);
                }

                $model->delete();

                $transaction->commit();

                Yii::app()->notification->message=Yii::t('slider','sliderRemoved',array('{slider}'=>$name));
            }catch(Exception $e){
                $transaction->rollback();
                Yii::app()->notification->error=Yii::t('slider','sliderNoRemoved',array('{slider}'=>$name)).$e->getMessage();
            }
        }

        $this->redirect($this->createUrl('index'));
    }

    public function actionSave()
    {
        $model=$this->model;
        $this->performAjaxValidation($model);

        if(isset($_POST['SliderBanner'])){

            $params=$_POST['SliderBanner'];

            if(isset($params['id']) && $params['id']){
                $model=$this->model->findByPk($params['id']);
            }

            if($model===null){
                Yii::app()->notification->error=Yii::t('error','noModel');
                $this->redirect($this->createUrl('index'));
            }

            $model->attr=$params;
            if($model->save()) Yii::app()->notification->message=Yii::t('slider','saveOk',array('{slide}'=>$model->name));
        }

        $this->redirect($this->createUrl('index'));
    }

    public function getModel()
    {
        if($this->__model===null) $this->__model=new SliderBanner();
        return $this->__model;
    }

    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='slider-form'){
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}